package com.inossem.warehousestorage_inossem.activity.analysis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.analysis.extra.AnalysisExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.analysis.WorkStatisticsFindPersonListAdapter;
import com.inossem.warehousestorage_inossem.bean.analysis.AnalysisWorkStatisticsFindPersonBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.analysis.AnalysisWorkStatisticsFindPersonRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

public class AnalysisWorkStatisticsFindPersonActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView personList;

    private List<AnalysisWorkStatisticsFindPersonBean.DataBean.ReceiverListBean> mReceiverList;

    private WorkStatisticsFindPersonListAdapter adapter;

    private String mCondition;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.activity_analysis_work_statistics_more_person);
        initView(AnalysisWorkStatisticsFindPersonActivity.this);
        initData(AnalysisWorkStatisticsFindPersonActivity.this);

    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        personList = (PullableListView) activity.findViewById(R.id.personList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.personListRefresh);
    }

    private void initData(Activity activity) {
        title.setText(activity.getResources().getString(R.string.analysis_work_statistics_more_person_title));
        ActivityUtils.initBackAndFunction(activity, R.string.analysis_work_statistics_more_person_back_title, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        personList.setPullUpFlag(false);
        personList.setPullDownFlag(false);
        resetData(activity);
//        setmScanListener((String scanResult, int length) -> {
//            getData(activity, QRCodeUtils.DecryptQRCode(scanResult));
//        }, QRCodeConstant.TYPE_URGENT);
    }

    private void resetData(Context context) {
        mCondition = "";
        mReceiverList = new ArrayList<>();
        addPersonListData(context, mReceiverList, personList, true);
    }

    private void addPersonListData(Context context, List<AnalysisWorkStatisticsFindPersonBean.DataBean.ReceiverListBean> personList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new WorkStatisticsFindPersonListAdapter(context, personList, mCondition, new AnalysisWorkStatisticsFindPersonListener() {
                @Override
                public void onNoDoubleClickListener(String condition) {
                    mCondition = condition;
                    getData(context, mCondition);
                }

                @Override
                public void selectPerson(String name, String id) {
                    Intent intent = new Intent();
                    intent.putExtra(AnalysisExtra.PERSON_NAME, name);
                    intent.putExtra(AnalysisExtra.PERSON_ID, id);
                    // 设置结果，并进行传送
                    AnalysisWorkStatisticsFindPersonActivity.this.setResult(Activity.RESULT_OK, intent);
                    finish();
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String condition) {
        AnalysisWorkStatisticsFindPersonRequest.request(context, condition, new GsonBeanListener<AnalysisWorkStatisticsFindPersonBean>() {
            @Override
            public void onSuccess(AnalysisWorkStatisticsFindPersonBean bean) {
                mReceiverList = bean.getData().getReceiverList();
                if (mReceiverList == null || mReceiverList.isEmpty()) {
                    Utils.middleToast(context, getResources().getString(R.string.activity_change_storage_materiel_list_undetected_data));
                }
                mCondition = condition;
                addPersonListData(context, ActivityUtils.getShowData(mReceiverList, 0), personList, true);
            }
        });
    }

    public interface AnalysisWorkStatisticsFindPersonListener {
        void onNoDoubleClickListener(String condition);

        void selectPerson(String name, String id);
    }
}
